<template>
  <div class="home">
		<el-container>
			<!-- 头 -->
		  <el-header>
			  <router-link tag="li" to="welcome">
			  <img src="../../public/22.png" style="width: 200px;margin-left: -20px;">
			  <span>后台管理系统</span></router-link>
			  <el-button type="info" @click="logout">退出</el-button>
		  </el-header>
		  <el-container>
			  <!-- 左侧边栏 -->
		    <el-aside :width=" isCollapse ? '64px' : '200px' ">
		        <div class="toggle-button" @click="toggleCollapse">|||</div>
		        <!-- 侧边栏 -->
		        <el-menu background-color="#373d41" text-color="#fff"
		         active-text-color="#409eff" unique-opened :collapse="isCollapse"
		         :collapse-transition="false" router :default-active="activePath">
		          
		      <el-submenu :index="item.id + '' " v-for="item in menulist" :key="item.id">
		       
		        <template slot="title">
		      
		          <i :class="iconsObj[item.id]"></i>
		        
		          <span>{{item.authName}}</span>
		        </template>
		    
		         
		          <el-menu-item :index="'/'+ subItem.path" v-for="subItem in item.children"
		           :key="subItem.id" @click="saveNavState('/'+ subItem.path)">
		            <template slot="title">
		            
		            <i class="el-icon-menu"></i>
		            
		            <span>{{subItem.authName}}</span>
		          </template>
		        </el-menu-item>
		      </el-submenu>
		    
		    </el-menu>
		    </el-aside>
			</el-aside>
			<!-- 右侧-->
		    <el-main>
				<router-view></router-view>
			</el-main>
		  </el-container>
		</el-container>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: "el-icon-user-solid",
        103: "el-icon-s-operation",
        101: "el-icon-document",
        102: "el-icon-notebook-1",
        145: "el-icon-loading",
      },
      // 是否折叠
      isCollapse: false,
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
 },
    // 点击按钮切换
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.home {
  width: 100%;
  height: 100%;
}
.el-container {
  width: 100%;
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-header span {
  color: white;
  font-size: 30px;
  position: absolute;
  transform: translate(0, 25%);
}
.el-button {
  float: right;
}
.el-aside {
  background-color: #373d41;
}
.el-aside .el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #373d41;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
li {
  list-style: none;
  margin-left: 0;
  cursor: pointer;
}
</style>
